package cn.sjxy.apiClient;

import cn.sjxy.baseLib.AjaxResult;
import cn.sjxy.baseLib.PageInfo;
import cn.sjxy.config.mvcExtend.NeedUserLogin;
import cn.sjxy.mapper.Mp_OrderInfo;
import cn.sjxy.mapper.Mp_User;
import cn.sjxy.model.Md_LoginUser;
import cn.sjxy.model.Md_OrderDetail;
import cn.sjxy.model.Md_OrderInfo;
import cn.sjxy.model.Md_User;
import cn.sjxy.service.Sv_OrderInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 客户端 - 订单API
 */
@RestController
@Validated
@Api(tags = "客户端-订单")
public class Ci_OrderInfo {

    @Autowired
    private Sv_OrderInfo sv_orderInfo;

    @Autowired
    private Mp_OrderInfo mp_orderInfo;

    @Autowired
    private Mp_User mp_user;

    /**
     * 图书详情页直接购买下单
     * @param request
     * @param bookCode
     * @param buyQty
     * @param receiveUser
     * @param receivePhone
     * @param receiveAddress
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/orderInfo/bookOrder")
    @ApiOperation("图书详情页直接购买")
    @NeedUserLogin
    public AjaxResult bookOrder(HttpServletRequest request,
                                @NotBlank(message = "图书编号不能为空") String bookCode,
                                @NotNull(message = "购买数量不能为空") Integer buyQty,
                                @NotBlank(message = "收货人不能为空") String receiveUser,
                                @NotBlank(message = "收货电话不能为空") String receivePhone,
                                @NotBlank(message = "收货地址不能为空") String receiveAddress) throws Exception {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");
        sv_orderInfo.bookOrder(loginUser.getUsername(), bookCode, buyQty, receiveUser, receivePhone, receiveAddress);
        return AjaxResult.success("下单成功");
    }

    /**
     * 购物车下单
     * @param request
     * @param bookCodeList
     * @param receiveUser
     * @param receivePhone
     * @param receiveAddress
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/orderInfo/shopOrder")
    @ApiOperation("购物车下单")
    @NeedUserLogin
    public AjaxResult shopOrder(HttpServletRequest request,
                                @RequestParam List<String> bookCodeList,
                                @NotBlank(message = "收货人不能为空") String receiveUser,
                                @NotBlank(message = "收货电话不能为空") String receivePhone,
                                @NotBlank(message = "收货地址不能为空") String receiveAddress) throws Exception {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");
        sv_orderInfo.shopOrder(loginUser.getUsername(), bookCodeList, receiveUser, receivePhone, receiveAddress);
        return AjaxResult.success("下单成功");
    }

    /**
     * 获取我的订单列表（分页）
     * @param request
     * @param orderCode
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/orderInfo/getTable")
    @ApiOperation("获取我的订单列表")
    @NeedUserLogin
    public AjaxResult getTable(HttpServletRequest request,
                               @RequestParam(required = false) String orderCode,
                               @RequestParam(defaultValue = "1") Integer pageIndex,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");

        // 构建查询条件
        QueryWrapper<Md_OrderInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", loginUser.getUsername());

        // 订单编号模糊查询
        if (orderCode != null && !orderCode.trim().isEmpty()) {
            wrapper.like("orderCode", orderCode);
        }

        // 按订单时间倒序
        wrapper.orderByDesc("orderDate");

        // 分页查询
        IPage<Md_OrderInfo> page = new Page<>(pageIndex, pageSize);
        page = mp_orderInfo.selectPage(page, wrapper);

        // 填充用户名称
        for (Md_OrderInfo orderInfo : page.getRecords()) {
            QueryWrapper<Md_User> userWrapper = new QueryWrapper<>();
            userWrapper.eq("userCode", orderInfo.getUserCode());
            Md_User user = mp_user.selectOne(userWrapper);
            if (user != null) {
                orderInfo.setUserName(user.getUserName());
            }
        }

        // 返回结果
        AjaxResult result = AjaxResult.success("查询成功");
        result.addTable(page.getRecords());
        result.addPage(page);
        return result;
    }

    /**
     * 获取订单详情
     * @param orderCode
     * @return
     */
    @GetMapping(value = "/orderInfo/getDetail")
    @ApiOperation("获取订单详情")
    @NeedUserLogin
    public AjaxResult getDetail(@NotBlank(message = "订单编号不能为空") String orderCode) {
        Md_OrderInfo orderInfo = sv_orderInfo.getOrderDetail(orderCode);
        List<Md_OrderDetail> detailList = sv_orderInfo.getOrderDetailList(orderCode);

        AjaxResult result = AjaxResult.success("查询成功");
        result.addKey("orderInfo", orderInfo);
        result.addKey("detailList", detailList);
        return result;
    }

    /**
     * 确认收货
     * @param request
     * @param orderCode
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/orderInfo/confirmReceive")
    @ApiOperation("确认收货")
    @NeedUserLogin
    public AjaxResult confirmReceive(HttpServletRequest request,
                                     @NotBlank(message = "订单编号不能为空") String orderCode) throws Exception {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");
        sv_orderInfo.confirmReceive(orderCode, loginUser.getUsername());
        return AjaxResult.success("确认收货成功");
    }
}

